package cn.iocoder.yudao.module.team.controller.admin.ledger;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.team.controller.admin.ledger.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.ledger.LedgerDO;
import cn.iocoder.yudao.module.team.service.ledger.LedgerService;

@Tag(name = "管理后台 - 报账管理")
@RestController
@RequestMapping("/team/ledger")
@Validated
public class LedgerController {

    @Resource
    private LedgerService ledgerService;

    @PostMapping("/create")
    @Operation(summary = "创建报账管理")
    @PreAuthorize("@ss.hasPermission('team:ledger:create')")
    public CommonResult<Integer> createLedger(@Valid @RequestBody LedgerSaveReqVO createReqVO) {
        return success(ledgerService.createLedger(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报账管理")
    @PreAuthorize("@ss.hasPermission('team:ledger:update')")
    public CommonResult<Boolean> updateLedger(@Valid @RequestBody LedgerSaveReqVO updateReqVO) {
        ledgerService.updateLedger(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报账管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('team:ledger:delete')")
    public CommonResult<Boolean> deleteLedger(@RequestParam("id") Integer id) {
        ledgerService.deleteLedger(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除报账管理")
                @PreAuthorize("@ss.hasPermission('team:ledger:delete')")
    public CommonResult<Boolean> deleteLedgerList(@RequestParam("ids") List<Integer> ids) {
        ledgerService.deleteLedgerListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报账管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('team:ledger:query')")
    public CommonResult<LedgerRespVO> getLedger(@RequestParam("id") Integer id) {
        LedgerDO ledger = ledgerService.getLedger(id);
        return success(BeanUtils.toBean(ledger, LedgerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报账管理分页")
    @PreAuthorize("@ss.hasPermission('team:ledger:query')")
    public CommonResult<PageResult<LedgerRespVO>> getLedgerPage(@Valid LedgerPageReqVO pageReqVO) {
        PageResult<LedgerDO> pageResult = ledgerService.getLedgerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LedgerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报账管理 Excel")
    @PreAuthorize("@ss.hasPermission('team:ledger:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLedgerExcel(@Valid LedgerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LedgerDO> list = ledgerService.getLedgerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报账管理.xls", "数据", LedgerRespVO.class,
                        BeanUtils.toBean(list, LedgerRespVO.class));
    }

}